package cn.kooki.app.duobao.ui.Adapter;

import android.content.Context;
import android.graphics.Color;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.format.DateUtils;
import android.text.style.ForegroundColorSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.drawable.GlideDrawable;
import com.bumptech.glide.request.animation.GlideAnimation;
import com.bumptech.glide.request.target.SimpleTarget;

import java.util.ArrayList;

import butterknife.Bind;
import butterknife.ButterKnife;

import cn.kooki.app.duobao.R;
import cn.kooki.app.duobao.data.Bean.detail.JionIninfo;
import cn.kooki.app.duobao.util.StringUtils;
import cn.kooki.app.duobao.util.TimeUtils;
import de.hdodenhof.circleimageview.CircleImageView;

/**
 * 夺宝记录列表adpter
 * Created by xionghao on 15/12/11.
 */
public class DetaillistAdpater extends BaseAdapter {

    private ArrayList<JionIninfo> datas;
    private Context context;

    public DetaillistAdpater(Context context, ArrayList<JionIninfo> datas) {
        this.context = context;
        this.datas = datas;
    }

    @Override
    public int getCount() {
        return datas.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        JionIninfo jionIninfo = datas.get(position);
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.layout_detailjoin_item, parent, false);
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        SpannableStringBuilder stringBuilder = new SpannableStringBuilder(
                jionIninfo.getUsername() + "(" + jionIninfo.getDizhi() + ")");
        stringBuilder.setSpan(new ForegroundColorSpan(Color.parseColor("#ff0079fc")), 0, jionIninfo.getUsername().length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        viewHolder.tvName.setText(stringBuilder);
        viewHolder.tvName.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });
        String timstring = TimeUtils.getdate(jionIninfo.getTime());
        viewHolder.tvDate.setText(timstring);
        if (getfristpostion(timstring) == position) {
            viewHolder.tvDate.setVisibility(View.VISIBLE);
        } else {
            viewHolder.tvDate.setVisibility(View.GONE);
        }
        final ViewHolder finalViewHolder = viewHolder;
        Glide.with(context).load(jionIninfo.getImg()).fitCenter().placeholder(R.drawable.img_blank).into(new SimpleTarget<GlideDrawable>() {
            @Override
            public void onResourceReady(GlideDrawable resource, GlideAnimation<? super GlideDrawable> glideAnimation) {
                finalViewHolder.ivAvatar.setImageDrawable(resource);
            }
        });
        String tvjoinifo = context.getString(R.string.time_string);
        tvjoinifo = String.format(tvjoinifo, jionIninfo.getGonumber(), TimeUtils.getTime(jionIninfo.getTime()));
        viewHolder.tvJoininfo.setText(tvjoinifo);
        return convertView;

    }

    public int getfristpostion(String time) {
        for (int i = 0; i < datas.size(); i++) {
            JionIninfo info = datas.get(i);
            if (TimeUtils.getdate(info.getTime()).equals(time)) {
                return i;
            }
        }
        return -1;
    }

    /**
     * This class contains all butterknife-injected Views & Layouts from layout file 'layout_detailjoin_item.xml'
     * for easy to all layout elements.
     *
     * @author ButterKnifeZelezny, plugin for Android Studio by Avast Developers (http://github.com/avast)
     */
    static class ViewHolder {
        @Bind(R.id.tv_date)
        TextView tvDate;
        @Bind(R.id.layout_date)
        LinearLayout layoutDate;
        @Bind(R.id.iv_avatar)
        CircleImageView ivAvatar;
        @Bind(R.id.tv_name)
        TextView tvName;
        @Bind(R.id.tv_joininfo)
        TextView tvJoininfo;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
